package com.example.mall.web.controller;

import com.example.mall.domain.Permission;
import com.example.mall.dto.PermissionDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PermissionQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IPermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    public IPermissionService permissionService;

    /**
     * 分页查询登录权限表信息
     * @param permissionQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询登录权限表信息",notes = "分页查询登录权限表信息",httpMethod = "GET")
    public JSONResult queryPage(PermissionQuery permissionQuery){
        PageDTO<PermissionDto> permissionDtoPageDTO = permissionService.queryPage(permissionQuery);
        return JSONResult.success("查询成功",permissionDtoPageDTO);
    }

    /**
     * 根据id查询登录权限表信息
     * @param permissionId
     * @return
     */
    @GetMapping("query/{permissionId}")
    @ApiOperation(value = "根据id查询登录权限表信息",notes = "根据id查询登录权限表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("permissionId")Long permissionId){
        //1.根据id查询用户信息
        if (permissionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.PERMISSIONID_NOT_ID);
        }
        Permission permission = permissionService.getById(permissionId);
        return JSONResult.success("查询成功",permission);
    }

    /**
     * 根据id查询登录权限表信息
     * @param permissionId
     * @return
     */
    @GetMapping("queryPermissionById/{permissionId}")
    @ApiOperation(value = "根据id查询登录权限表信息",notes = "根据id查询登录权限表信息",httpMethod = "GET")
    public JSONResult queryPermissionById(@PathVariable("permissionId")Long permissionId){
        //1.根据id查询用户信息
        if (permissionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.PERMISSIONID_NOT_ID);
        }
        Permission permission = permissionService.queryPermissionById(permissionId);
        return JSONResult.success("查询成功",permission);
    }

    /**
     * 查询全部登录权限表信息
     * @param
     * @return
     */
    @GetMapping("queryPermissionList")
    @ApiOperation(value = "查询全部登录权限表信息",notes = "查询全部登录权限表信息",httpMethod = "GET")
    public JSONResult queryPermissionList(){
        List<Permission> permissions = permissionService.queryPermissionList();
        return JSONResult.success("查询成功",permissions);
    }

    /**
     * 新增登录权限表
     * @param permissionDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增登录权限表",notes = "新增登录权限表",httpMethod = "POST")
    public JSONResult insert(@RequestBody PermissionDto permissionDto){
        permissionService.insert(permissionDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改登录权限表信息
     * @param permissionDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改登录权限表信息",notes = "修改登录权限表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody PermissionDto permissionDto){
        permissionService.update(permissionDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除登录权限表信息
     * @param permissionId
     * @return
     */
    @PostMapping("delete/{permissionId}")
    @ApiOperation(value = "删除登录权限表信息",notes = "删除登录权限表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("activitiesProductId") Long permissionId) {
        permissionService.delete(permissionId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除登录权限表信息
     * @param permissionIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除登录权限表信息",notes = "批量删除登录权限表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> permissionIds) {
        permissionService.deleteBatch(permissionIds);
        return JSONResult.success("批量删除成功");
    }
}
